Imports AvaluacionsDB
''' Clase exemple 3.
Public Class AvaluacioFinal
    'variables
    Private NombreCompensables As Int32
    Private NotaCompensa As Double
    Private Alumnes As DataTable
    Private NotesAlumnes As DataTable


    Public Function AvaluarAlumnes(ByVal NombreCompensables As Int32, ByVal NotaCompensa As Double) As DataTable
        'Primer obtenim totes les dades i taules necessaries
        Me.NombreCompensables = NombreCompensables
        Me.NotaCompensa = NotaCompensa
        'Aqui obtenim els alumnes i possem les columnes que ens calen
        Alumnes = ObtenirAlumnes()
        NotesAlumnes = ObtenirNotesAlumnes()
        'Bucle principal
        For Each Alumne As DataRow In Alumnes.Rows
            CalcularNotesAlumne(Alumne)
        Next
        Return Alumnes
    End Function
    Public Function ObtenirAlumnes() As DataTable
        Dim AlumnesSQL As DataTable = AlumnesDAO.LlistatAlumnesNom
        'Camps calculats
        AlumnesSQL.Columns.Add("NotaPromig", Type.GetType("System.Double"))
        AlumnesSQL.Columns.Add("AsignaturesCompensades", Type.GetType("System.Int32"))
        AlumnesSQL.Columns.Add("AsignaturesAprovades", Type.GetType("System.Int32"))
        AlumnesSQL.Columns.Add("AsignaturesCursades", Type.GetType("System.Int32"))
        AlumnesSQL.Columns.Add("Aprova", Type.GetType("System.Boolean"))
        Return AlumnesSQL
    End Function

    Public Function ObtenirNotesAlumnes() As DataTable
        Return NotesDAO.NotesAlumnes
    End Function

    Public Sub CalcularNotesAlumne(ByRef FilaAlumne As DataRow)
        'No retornem res, fem servir el mateix datarow pel retorn
        'Contadors de cada alumne
        Dim NotaPromig As Double = 0.0
        Dim AsignaturesCompensades As Int32 = 0
        Dim AsignaturesAprovades As Int32 = 0
        Dim AsignaturesCursades As Int32 = 0
        Dim Aprova As Boolean = False

        For Each Nota As DataRow In NotesAlumnes.Rows
            'Ens quedem amb les notes de cada alumne
            If Nota("id_alumne") = FilaAlumne("id_alumne") Then
                'acumuladors
                NotaPromig += CDbl(Nota("notafinal"))
                AsignaturesCursades += 1
                If Nota("notafinal") >= 5.0 Then
                    AsignaturesAprovades += 1
                ElseIf Nota("notafinal") >= NotaCompensa And AsignaturesCompensades < NombreCompensables Then
                    AsignaturesCompensades += 1
                End If
            End If
        Next
        'Calcul final
        If AsignaturesCursades > 0 Then
            NotaPromig = NotaPromig / AsignaturesCursades
            If AsignaturesCursades = AsignaturesCompensades + AsignaturesAprovades Then
                Aprova = True
            End If
        End If
        'Actualitzar el datarow, el passem per referencia 
        FilaAlumne("NotaPromig") = NotaPromig
        FilaAlumne("AsignaturesCompensades") = AsignaturesCompensades
        FilaAlumne("AsignaturesAprovades") = AsignaturesAprovades
        FilaAlumne("AsignaturesCursades") = AsignaturesCursades
        FilaAlumne("Aprova") = Aprova

    End Sub
End Class
